package my.edu.model.bills.sales.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import my.edu.aspect.LogExecutionTime;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.Response;
import my.edu.common.validate.groups.Insert;
import my.edu.common.validate.groups.Sales;
import my.edu.constant.DeliveryMethod;

import my.edu.model.bills.purchase.entity.dto.PurchaseOrderSearchDTO;
import my.edu.model.bills.sales.entity.dto.SalesDTO;
import my.edu.model.bills.sales.entity.dto.SalesOrderSearchDTO;
import my.edu.model.bills.sales.service.ISalesOrderService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salesOrder")
@Tag(name = "销售订单管理")
@Validated
public class SalesOrderController {
    @Autowired
    private ISalesOrderService salesOrderService;
    @Autowired
    private DeliveryMethod deliveryMethod;

    @PutMapping
    @RequiresPermissions("sales:save")
    @Operation(summary = "新增订单")
    public Response addSalesOrder(@RequestBody @Validated(value = {Insert.class}) SalesDTO salesDTO, @RequestParam(required = false) String status) {
        salesOrderService.saveNew(salesDTO, status);
        return Response.success();
    }

    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("sales:del")
    @Operation(summary = "批量删除订单")
    public Response delSalesOrder(String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        salesOrderService.removeOrder(longs);
        return Response.success();
    }

    @DeleteMapping("/delete")
    @RequiresPermissions("sales:del")
    @Operation(summary = "删除订单")
    public Response delete(Long id) {
        salesOrderService.removeOrder(List.of(id));
        return Response.success();
    }

    @PostMapping("/update")
    @RequiresPermissions("sales:update")
    @Operation(summary = "修改订单")
    public Response updateSalesOrder(@RequestBody @Validated SalesDTO salesDTO, @RequestParam(required = false) String status) {
        salesOrderService.updateOrder(salesDTO, status);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"purchase:audit"})
    @Operation(summary = "更新审核状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        List<Long> longs = StringUtils.strToLongList(ids);
        if(status==1){
            salesOrderService.auditOrder(longs);
        } else if (status==0) {
            salesOrderService.rauditOrder(longs);
        } else{
            return Response.error("状态值错误");
        }
        return Response.success();
    }

    @GetMapping("/list")
    @RequiresPermissions("sales:get")
    @Operation(summary = "查询订单")
    public Response getSalesOrder(@ModelAttribute @Validated PageDTO page,
                                  @ModelAttribute SalesOrderSearchDTO salesOrderSearchDTO) {
        return Response.success(salesOrderService.getSalesOrderList(page, salesOrderSearchDTO));
    }

    @GetMapping("/one")
    @RequiresPermissions("sales:get")
    @Operation(summary = "根据id查询订单")
    public Response getSalesOrder( @NotNull(message = "id不能为空") Long id) {
        return Response.success(salesOrderService.getSalesOrderOne(id));
    }

    @GetMapping
    @Operation(summary = "查询全部订单")
    public Response getOnly(@ModelAttribute SalesOrderSearchDTO salesOrderSearchDTO) {
        return Response.success( salesOrderService.listOnly(salesOrderSearchDTO));
    }

    @LogExecutionTime
    @GetMapping("/saleOut")
    @RequiresPermissions("sales:get")
    @Operation(summary = "查询报表")
    public Response getPurchaseReport(@ModelAttribute SalesOrderSearchDTO salesOrderSearchDTO,
                                      @ModelAttribute PageDTO pageDTO) {
        return Response.success(salesOrderService.getReportList(pageDTO,salesOrderSearchDTO));
    }

}
